package com.risetechnology.krystalkitchen1;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.risetechnology.common.Const;
import com.risetechnology.master.NavigationGroup;
import com.risetechnology.utils.CropOption;
import com.risetechnology.utils.CropOptionAdapter;

public class GroupRecipes extends NavigationGroup {
	@SuppressWarnings("deprecation")
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		Intent intent = new Intent(this, ActivityRecipes.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_SINGLE_TOP);
		View view = getLocalActivityManager().startActivity(Const.TAB_TIPS,
				intent).getDecorView();
		replaceView(view, Const.TAB_TIPS);
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode != RESULT_OK)
			return;

		switch (requestCode) {
		case ActivityCreateNewRecipe.REQUEST_CODE_CAMERA:
			doCropCreate();
			break;
		case ActivityCreateNewRecipe.REQUEST_CODE_GALLERY:
			ActivityCreateNewRecipe.uriPicture = data.getData();
			doCropCreate();
			break;

		case ActivityCreateNewRecipe.REQUEST_CODE_CROP:
			Bundle extras = data.getExtras();

			if (extras != null) {
				Bitmap photo = extras.getParcelable("data");

				ActivityCreateNewRecipe.imageviewPhoto.setImageBitmap(photo);
			}

			File f = new File(ActivityCreateNewRecipe.uriPicture.getPath());

			if (f.exists()) {
				f.delete();
			}

			break;

		case ActivityEditRecipe.REQUEST_CODE_CAMERA:
			doCropEdit();
			break;
		case ActivityEditRecipe.REQUEST_CODE_GALLERY:
			ActivityEditRecipe.uriPicture = data.getData();
			doCropEdit();
			break;

		case ActivityEditRecipe.REQUEST_CODE_CROP:
			Bundle extras1 = data.getExtras();

			if (extras1 != null) {
				Bitmap photo = extras1.getParcelable("data");

				ActivityEditRecipe.imageviewPhoto.setImageBitmap(photo);
			}

			File f1 = new File(ActivityEditRecipe.uriPicture.getPath());

			if (f1.exists()) {
				f1.delete();
			}

			break;
		}
		Activity activity = getLocalActivityManager().getCurrentActivity();
		if (activity != null && activity instanceof ActivityCreateNewRecipe) {
			((ActivityCreateNewRecipe) activity).onActivityResult(requestCode,
					resultCode, data);
		}
	}

	private void doCropCreate() {
		final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();

		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setType("image/*");

		List<ResolveInfo> list = getPackageManager().queryIntentActivities(
				intent, 0);

		int size = list.size();

		if (size == 0) {
			Toast.makeText(this, "Can not find image crop app",
					Toast.LENGTH_SHORT).show();

			return;
		} else {
			intent.setData(ActivityCreateNewRecipe.uriPicture);

			intent.putExtra("outputX", 300);
			intent.putExtra("outputY", 300);
			intent.putExtra("aspectX", 1);
			intent.putExtra("aspectY", 1);
			intent.putExtra("scale", true);
			intent.putExtra("return-data", true);

			if (size == 1) {
				Intent i = new Intent(intent);
				ResolveInfo res = list.get(0);

				i.setComponent(new ComponentName(res.activityInfo.packageName,
						res.activityInfo.name));

				startActivityForResult(i,
						ActivityCreateNewRecipe.REQUEST_CODE_CROP);
			} else {
				for (ResolveInfo res : list) {
					final CropOption co = new CropOption();

					co.title = getPackageManager().getApplicationLabel(
							res.activityInfo.applicationInfo);
					co.icon = getPackageManager().getApplicationIcon(
							res.activityInfo.applicationInfo);
					co.appIntent = new Intent(intent);

					co.appIntent
							.setComponent(new ComponentName(
									res.activityInfo.packageName,
									res.activityInfo.name));

					cropOptions.add(co);
				}

				CropOptionAdapter adapter = new CropOptionAdapter(
						getApplicationContext(), cropOptions);

				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle("Choose Crop App");
				builder.setAdapter(adapter,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int item) {
								startActivityForResult(
										cropOptions.get(item).appIntent,
										ActivityCreateNewRecipe.REQUEST_CODE_CROP);
							}
						});

				builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
					@Override
					public void onCancel(DialogInterface dialog) {

						if (ActivityCreateNewRecipe.uriPicture != null) {
							getContentResolver().delete(
									ActivityCreateNewRecipe.uriPicture, null,
									null);
							ActivityCreateNewRecipe.uriPicture = null;
						}
					}
				});

				AlertDialog alert = builder.create();

				alert.show();
			}
		}
	}

	private void doCropEdit() {
		final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();

		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setType("image/*");

		List<ResolveInfo> list = getPackageManager().queryIntentActivities(
				intent, 0);

		int size = list.size();

		if (size == 0) {
			Toast.makeText(this, "Can not find image crop app",
					Toast.LENGTH_SHORT).show();

			return;
		} else {
			intent.setData(ActivityEditRecipe.uriPicture);

			intent.putExtra("outputX", 300);
			intent.putExtra("outputY", 300);
			intent.putExtra("aspectX", 1);
			intent.putExtra("aspectY", 1);
			intent.putExtra("scale", true);
			intent.putExtra("return-data", true);

			if (size == 1) {
				Intent i = new Intent(intent);
				ResolveInfo res = list.get(0);

				i.setComponent(new ComponentName(res.activityInfo.packageName,
						res.activityInfo.name));

				startActivityForResult(i, ActivityEditRecipe.REQUEST_CODE_CROP);
			} else {
				for (ResolveInfo res : list) {
					final CropOption co = new CropOption();

					co.title = getPackageManager().getApplicationLabel(
							res.activityInfo.applicationInfo);
					co.icon = getPackageManager().getApplicationIcon(
							res.activityInfo.applicationInfo);
					co.appIntent = new Intent(intent);

					co.appIntent
							.setComponent(new ComponentName(
									res.activityInfo.packageName,
									res.activityInfo.name));

					cropOptions.add(co);
				}

				CropOptionAdapter adapter = new CropOptionAdapter(
						getApplicationContext(), cropOptions);

				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle("Choose Crop App");
				builder.setAdapter(adapter,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int item) {
								startActivityForResult(
										cropOptions.get(item).appIntent,
										ActivityEditRecipe.REQUEST_CODE_CROP);
							}
						});

				builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
					@Override
					public void onCancel(DialogInterface dialog) {

						if (ActivityEditRecipe.uriPicture != null) {
							getContentResolver().delete(
									ActivityEditRecipe.uriPicture, null, null);
							ActivityEditRecipe.uriPicture = null;
						}
					}
				});

				AlertDialog alert = builder.create();

				alert.show();
			}
		}
	}
}
